function ListNode(val) {
  this.val = val;
  this.next = null;
}

//添加节点
function appendNode(head, val) {
  let newNode = new ListNode(val);
  if (!head) {
    return newNode;
  }
  let current = head;
  while (current.next) {
    current = current.next;
  }
  current.next = newNode;
  return head; // 返回头节点
}

//打印链表
function printList(head) {
  let current = head;
  while (current) {
    console.log(current.val);
    current = current.next;
  }
}


function createCycleLinkedList(values){
    let nodes = values.map(val => new ListNode(val))
    for (let i = 0; i < nodes.length - 1; i++) {
        nodes[i].next = nodes[i + 1];
    }
    nodes[nodes.length - 1].next = nodes[1];
    return nodes[0];
}


let values = [3, 2, 0, -4];
let head = createCycleLinkedList(values)
// printList(head)

let hasCycle = function(head){
  let current = head
  let hash = new Map()
  while(current){
    if(hash.get(current.val)){
      return true
    }else{
      hash.set(current.val, 1)
    }
    current = current.next
  }
  return false

}

console.log(hasCycle(head))